package com.heihei.util;

import java.io.*;
import java.util.Properties;
/**
 *  用于读取配置文件
 *
 * @version 1.0
 * @since JDK1.8
 * @author duanhengfei
 * @date 2021年01月29日 19:46:39
 */
public class ConfigUtilF {
    private static Properties properties;

    public ConfigUtilF() {
    }

    static {
        init();
    }

    public static void init() {
        Properties p = new Properties();
        String resourcePath = null;
        try {
            //读取配置文件时，优先先读与jar包同级的配置文件，
            //如果jar包同级目录中，没有 script.properties 配置文件，就从资源文件中读取
            File file = new File(".","script.properties");
            if(file.exists()) {
                BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
                p.load(bufferedReader);
            } else {
                System.out.println("未读取到一级优先配置文件：" + file.getAbsolutePath());
                resourcePath = ConfigUtilF.class.getClassLoader().getResource("script.properties").getPath();
                p.load(new BufferedReader(new InputStreamReader(new FileInputStream(resourcePath), "utf-8")));
            }
        } catch (IOException var4) {
            var4.printStackTrace();
            System.out.println("二级右键配置文件亦读取失败：" + resourcePath);
            System.out.println("请检查配置文件是否正常！！！");
        }
        properties = p;
    }

    public static Properties getProperties() {
        return properties;
    }
}

